/*
 * Plot.m
 * Copyright 2008 . All rights reserved.
 */
//  Modified to make compile under SDK 3.1 by Olaf Greck in May 2010
//
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


#import "Plot.h"
#import "FuelLogAppDelegate.h"

@implementation Plot

@synthesize beginIdx, endIdx;

-(void)drawView:(QuartzView*)view inContext:(CGContextRef)context bounds:(CGRect)bounds
{
	// do the math first
	FuelLogAppDelegate *appDelegate = (FuelLogAppDelegate *)[[UIApplication sharedApplication] delegate];
	int prefUnits = [FuelLogAppDelegate pref].units;
	BOOL uk = (prefUnits == UK);
	BOOL norway = (prefUnits == NORWAY);
	BOOL inverted = (prefUnits == INV_METRIC || norway);
	Gas *gas;
	for(; endIdx <= beginIdx && endIdx < appDelegate.pumps.count; ++endIdx) {
		gas = (Gas *)[appDelegate.pumps objectAtIndex:endIdx];
		if (gas.full) break;
	}
	for(; beginIdx >= endIdx && beginIdx >= 0; --beginIdx) {
		gas = (Gas *)[appDelegate.pumps objectAtIndex:beginIdx];
		if (gas.full) break;
	}
	float fuel = 0.0;
	float distance = gas.odometer;
	NSMutableArray *fillups = [NSMutableArray arrayWithCapacity:10];
	float dpf, minDpf = 9999999.0, maxDpf = 0.0;
	--beginIdx;
	for(; beginIdx >= endIdx; --beginIdx) {
		gas = (Gas *)[appDelegate.pumps objectAtIndex:beginIdx];		
		fuel += gas.volume;
		if (gas.full) {
			dpf = (gas.odometer - distance) / fuel;
			if (inverted && dpf > 0.0) {
				if (norway) {
					dpf = 10.0 / dpf;
				} else {
					dpf = 100.0 / dpf;
				}
			} else if (uk) {
				dpf = dpf * UK_GALLON;
			}
			if (dpf < minDpf) minDpf = dpf;
			if (dpf > maxDpf) maxDpf = dpf;
			[fillups addObject:[NSNumber numberWithFloat:dpf]];
			distance = gas.odometer;
			fuel = 0.0;
		}
	}
	if (norway) {
		maxDpf = ceil(maxDpf*10) / 10.0;
		minDpf = floor(minDpf*10) / 10.0;
	} else {
		maxDpf = ceil(maxDpf);
		minDpf = floor(minDpf);
	}
	
#define WIDTH 25
#define TOP_OFFSET 20
#define BOTTOM_OFFSET 30
#define HOFFSET 5
	
	float vspace = (bounds.size.height - TOP_OFFSET - BOTTOM_OFFSET)/3;
	int vtick0 = TOP_OFFSET;
	int vtick1 = vtick0 + vspace;
	int vtick3 = bounds.size.height - BOTTOM_OFFSET;
	int vtick2 = vtick3 - vspace;
	
	float markSize = (maxDpf - minDpf)/3;
	float mark0 = maxDpf;
	float mark1 = maxDpf - markSize;
	float mark3 = minDpf;
	float mark2 = minDpf + markSize;
	
	// draw background in light blue
	CGContextSetRGBFillColor(context, 0.2, 0.4, 0.75, 1.0);
	CGContextFillRect(context, CGRectMake(0.0, 0.0, bounds.size.width, bounds.size.height));

	NSNumberFormatter *yFormatter = [[FuelLogAppDelegate pref].distanceFormatter retain];
	if (norway) {
		yFormatter = [[NSNumberFormatter alloc] init];
		[yFormatter setPositiveFormat:DECIMAL_1];
	}
	NSString *s0 = [yFormatter stringFromNumber:[NSNumber numberWithFloat:mark0]];
	NSString *s1 = [yFormatter stringFromNumber:[NSNumber numberWithFloat:mark1]];
	NSString *s2 = [yFormatter stringFromNumber:[NSNumber numberWithFloat:mark2]];
	NSString *s3 = [yFormatter stringFromNumber:[NSNumber numberWithFloat:mark3]];

	UIFont *font = [UIFont systemFontOfSize:10];
	CGFloat actualFontSize;
	CGSize size;
	size = [s0 sizeWithFont:font minFontSize:10 actualFontSize:&actualFontSize forWidth:WIDTH lineBreakMode:UILineBreakModeTailTruncation];

	UIColor *textColor = [UIColor whiteColor];
	[textColor set];
	CGPoint point;
	point = CGPointMake(0 + HOFFSET + WIDTH - size.width, vtick0 - size.height/2);
	[s0 drawAtPoint:point forWidth:WIDTH withFont:font minFontSize:actualFontSize actualFontSize:&actualFontSize lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
	point = CGPointMake(0 + HOFFSET + WIDTH - size.width, vtick1 - size.height/2);
	[s1 drawAtPoint:point forWidth:WIDTH withFont:font minFontSize:actualFontSize actualFontSize:&actualFontSize lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
	point = CGPointMake(0 + HOFFSET + WIDTH - size.width, vtick2 - size.height/2);
	[s2 drawAtPoint:point forWidth:WIDTH withFont:font minFontSize:actualFontSize actualFontSize:&actualFontSize lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
	point = CGPointMake(0 + HOFFSET + WIDTH - size.width, vtick3 - size.height/2);
	[s3 drawAtPoint:point forWidth:WIDTH withFont:font minFontSize:actualFontSize actualFontSize:&actualFontSize lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];	

	NSString *xlabel = (NSLocalizedString(@"Plot Date",@"The word 'date' shown on the graph."));
	size = [xlabel sizeWithFont:font minFontSize:10 actualFontSize:&actualFontSize forWidth:bounds.size.width lineBreakMode:UILineBreakModeTailTruncation];
	point = CGPointMake((bounds.size.width - size.width)/2, bounds.size.height - 16);
	[xlabel drawAtPoint:point forWidth:size.width withFont:font minFontSize:actualFontSize actualFontSize:&actualFontSize lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];	
		
	NSString *title = (NSLocalizedString(@"Plot Fuel Efficiency",@"The words 'Fuel Efficiency' shown on the graph"));
	font = [UIFont	systemFontOfSize:12];
	size = [title sizeWithFont:font minFontSize:12 actualFontSize:&actualFontSize forWidth:bounds.size.width lineBreakMode:UILineBreakModeTailTruncation];
	point = CGPointMake((bounds.size.width - size.width)/2, 5);
	[title drawAtPoint:point forWidth:size.width withFont:font minFontSize:actualFontSize actualFontSize:&actualFontSize lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];	
	
	CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
	CGContextSetLineWidth(context, 2.0);

	float dpfrange = maxDpf - minDpf;
	int yrange = vtick3 - vtick0;
	int startx = HOFFSET + WIDTH + 5;
	CGContextMoveToPoint(context, startx, 5);
	CGContextAddLineToPoint(context, startx, bounds.size.height - BOTTOM_OFFSET + 10);
	CGContextAddLineToPoint(context, bounds.size.width - 5, bounds.size.height - BOTTOM_OFFSET + 10);
	CGContextStrokePath(context);
	
	CGContextSetLineWidth(context, 1.0);
	int endx = bounds.size.width - HOFFSET;
	float xspace = (endx - startx - 5) / (fillups.count - 1);
	startx += 5;
	CGContextMoveToPoint(context, startx, vtick3 - ([(NSNumber *)[fillups objectAtIndex:0] floatValue] - minDpf)/dpfrange * yrange);
	for (int i = 1; i < fillups.count; ++i) {
		startx += xspace;
		CGContextAddLineToPoint(context, startx, vtick3 - ([(NSNumber *)[fillups objectAtIndex:i] floatValue] - minDpf)/dpfrange * yrange);
	}
	CGContextStrokePath(context);
	[yFormatter release];
}

@end
